/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package studentdata;

import java.util.Vector;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

/**
 *
 * @author Administrator
 */
public class XMLHandler extends DefaultHandler{
    XMLHandler()
    {
        super();
    }

    @Override public void startDocument()
    {
        System.out.println("Rozpoczynam odczyt dokumentu");
    }

    @Override public void endDocument()
    {
        System.out.println("Zakonczylem odczyt dokumentu");
    }

    @Override public void startElement(String uri, String name, String qName, Attributes atts)
    {
        System.out.println("Start: " + uri + " " + name + " " + qName);
        if(name.equals("student"))
        {
            xmlPath = "student";
        }
        else
        {
            xmlPath += "/" + name;
        }

        if(name.equals("dormitory"))
        {
            student.setRoomNumber(atts.getValue("roomNumber"));
        }

        if(name.equals("workflowNode"))
        {
            workflow = student.getWorkflow();
            node = new WorkflowNode();
            node.fromID = Student.StrToWorkID(atts.getValue("fromID"));
            node.recipientID = Student.StrToWorkID(atts.getValue("recipientID"));
            node.toID = Student.StrToWorkID(atts.getValue("toID"));
            node.sendDate = atts.getValue("sendDate");


        }
    }

    @Override public void endElement(String uri, String name, String qName)
    {
        System.out.println("End: " + uri + " " + name + " " + qName);
        if(name.equals("student"))
        {
            xmlPath = "";
        }
        else
        {
            xmlPath = xmlPath.substring(0, xmlPath.lastIndexOf(name)-1);
        }

        if(name.equals("workflowNode"))
        {
            workflow.add(node);
            student.setWorkflow(workflow);

            node = null;
            workflow = null;
        }
    }

    @Override public void characters(char ch[], int start, int length)
    {
        String chars = "";
        for(int i = start; i<start+length; i++)
        {
            switch(ch[i])
            {
                case '\t':
                    break;
                default:
                    chars += ch[i];
                    break;
            }
        }

        //decide where to save the string
        if(xmlPath.equals("student/personalData/name"))
        {
            student.setName(chars);
        }
        if(xmlPath.equals("student/personalData/surname"))
        {
            student.setSurname(chars);
        }
        if(xmlPath.equals("student/personalData/peselNumber"))
        {
            student.setPeselNumber(chars);
        }
        if(xmlPath.equals("student/personalData/address"))
        {
            student.setAddress(chars);
        }
        if(xmlPath.equals("student/universityData/faculty"))
        {
            student.setFaculty(chars);
        }
        if(xmlPath.equals("student/universityData/dormitory"))
        {
            student.setDormitoryCode(chars);
        }
        if(xmlPath.equals("student/universityData/faculty"))
        {
            student.setFaculty(chars);
        }
        if(xmlPath.equals("student/workflow/workflowNode"))
        {
            node.recipientName = chars;
        }
    }

    private Student student;
    public void setStudent(Student student)
    {
        this.student = student;
    }
    public Student getStudent(){
        return student;
    }

    private String xmlPath = ""; //path inside the document
    Vector workflow = null;
    WorkflowNode node = null;
}
